package com.tjsinfo.tjsoa.system.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.tjsinfo.tjsoa.Dao.HQLDao;
import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.Dao.ISpringDao;
import com.tjsinfo.tjsoa.check.vo.TCheckLead;
import com.tjsinfo.tjsoa.mail.vo.Tjsmail;
import com.tjsinfo.tjsoa.system.vo.TSystemUser;
import com.tjsinfo.tjsoa.system.vo.TSystemUserRoleGroup;

/**
 * A data access object (DAO) providing persistence and search support for
 * TSystemUser entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.tjsinfo.tjsoa.system.vo.TSystemUser
 * @author MyEclipse Persistence Tools
 */

public class TSystemUserDao extends HibernateDaoSupport implements ISystemUserDao {
	private static final Log log = LogFactory.getLog(TSystemUserDao.class);
	private ISpringDao springDao;
	
	public ISpringDao getSpringDao() {
		return springDao;
	}

	public void setSpringDao(ISpringDao springDao) {
		this.springDao = springDao;
	}

	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.system.dao.ISystemUserDao#save(com.tjsinfo.tjsoa.system.vo.TSystemUser)
	 */
	public Integer save(TSystemUser transientInstance) {
		log.debug("saving TSystemUser instance");
		Integer id =0;
		try {
			id = (Integer) getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
		return id;
	}

	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.system.dao.ISystemUserDao#delete(com.tjsinfo.tjsoa.system.vo.TSystemUser)
	 */
	public void delete(TSystemUser persistentInstance) {
		log.debug("deleting TSystemUser instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.system.dao.ISystemUserDao#findById(java.lang.Integer)
	 */
	public TSystemUser findById(java.lang.Integer id) {
		log.debug("getting TSystemUser instance with id: " + id);
		try {
			TSystemUser instance = (TSystemUser) getHibernateTemplate().get(
					"com.tjsinfo.tjsoa.system.vo.TSystemUser", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public TSystemUser findByuserId(java.lang.Integer userId) {
		log.debug("getting TSystemUser instance with userId: " + userId);
		try {
			TSystemUser instance = (TSystemUser) getHibernateTemplate().get(
					"com.tjsinfo.tjsoa.system.vo.TSystemUser", userId);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	public List<TSystemUserRoleGroup> selectGroupsByRoleGroup(Integer roleGroupId) {
		// TODO Auto-generated method stub
		String queryString="select *  from dbo.t_system_userRoleGroup where roleGroupId=? and userId IN (select userId from  dbo.t_system_user) order by userId desc ";
		List<TSystemUserRoleGroup> systemUserRole = springDao.querySQL(queryString, new Object[]{roleGroupId}, new RowMapper(){
			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				TSystemUserRoleGroup systemUserRoleGroup = new TSystemUserRoleGroup();
				systemUserRoleGroup.setId(arg0.getInt("id"));
				systemUserRoleGroup.setRoleGroupId(arg0.getInt("roleGroupId"));
				systemUserRoleGroup.setUserId(arg0.getInt("userId"));
				return systemUserRoleGroup;
			}
		});
		return systemUserRole;
	}
	public List<TSystemUser> selectByRoleGroup(Integer roleGroupId) {
		// TODO Auto-generated method stub
		String queryString="select * from dbo.t_system_user where userId IN (select userId from  dbo.t_system_userRoleGroup where roleGroupId=? ) order by orderNo asc ";
		List<TSystemUser> systemUser = springDao.querySQL(queryString, new Object[]{roleGroupId}, new RowMapper(){
			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				TSystemUser systemUsers = new TSystemUser();
				systemUsers.setUserId(arg0.getInt("userId"));
				systemUsers.setUuid(arg0.getString("uuid"));
				systemUsers.setLoginName(arg0.getString("loginName"));
				systemUsers.setPassword(arg0.getString("password"));
				systemUsers.setState(arg0.getByte("state"));
				systemUsers.setUserName(arg0.getString("userName"));
				systemUsers.setEmail(arg0.getString("email"));
				systemUsers.setOrderNo(arg0.getInt("orderNo"));
				return systemUsers;
			}
		});
		return systemUser;
	}
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.system.dao.ISystemUserDao#findByExample(com.tjsinfo.tjsoa.system.vo.TSystemUser)
	 */
	public List findByExample(TSystemUser instance) {
		log.debug("finding TSystemUser instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.system.dao.ISystemUserDao#findByProperty(java.lang.String, java.lang.Object)
	 */
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding TSystemUser instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from TSystemUser as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.system.dao.ISystemUserDao#findAll()
	 */
	public List findAll() {
		log.debug("finding all TSystemUser instances");
		try {
			String queryString = "from TSystemUser";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.system.dao.ISystemUserDao#merge(com.tjsinfo.tjsoa.system.vo.TSystemUser)
	 */
	public TSystemUser merge(TSystemUser detachedInstance) {
		log.debug("merging TSystemUser instance");
		try {
			TSystemUser result = (TSystemUser) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.system.dao.ISystemUserDao#attachDirty(com.tjsinfo.tjsoa.system.vo.TSystemUser)
	 */
	public void attachDirty(TSystemUser instance) {
		log.debug("attaching dirty TSystemUser instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.system.dao.ISystemUserDao#attachClean(com.tjsinfo.tjsoa.system.vo.TSystemUser)
	 */
	public void attachClean(TSystemUser instance) {
		log.debug("attaching clean TSystemUser instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static ISystemUserDao getFromApplicationContext(
			ApplicationContext ctx) {
		return (ISystemUserDao) ctx.getBean("TSystemUserDAO");
	}
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.system.dao.ISystemUserDao#merge(com.tjsinfo.tjsoa.system.vo.TSystemUser)
	 */
	public Boolean update(TSystemUser detachedInstance) {
		log.debug("merging TSystemUser instance");
		try {
			 getHibernateTemplate().update(
					detachedInstance);
			log.debug("merge successful");
			return true;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			return false;
			
			
		}
	}

}